package com.parking.renters.controller;


import cn.hutool.core.bean.BeanUtil;
import com.parking.common.core.model.PageInfo;
import com.parking.common.core.model.Result;
import com.parking.common.core.util.JwtUtil;
import com.parking.common.web.aop.Permission;
import com.parking.common.web.util.RedisUtil;
import com.parking.renters.entity.Renters;
import com.parking.renters.exception.RentersException;
import com.parking.renters.exceptionCode.RentersExceptionCode;
import com.parking.renters.model.stallCall.dto.RentersDto;
import com.parking.renters.model.stallCall.dto.RentersInfoDto;
import com.parking.renters.model.stallCall.dto.RentersLoginDTO;
import com.parking.renters.model.stallCall.dto.SelectRentersDTO;
import com.parking.renters.model.stallCall.form.RegisterForm;
import com.parking.renters.model.stallCall.form.RentersLoginForm;
import com.parking.renters.model.stallCall.form.RentersPhoneForm;
import com.parking.renters.model.stallCall.form.SelectRenterForm;
import com.parking.renters.service.RentersService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 彭于晏
 * @since 2023年11月29日
 */
@RestController
@RequestMapping("/renters")
@Api(tags = "出租客账户管理")
public class RentersController {
    @Autowired
    private  RentersService rentersService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 车位模块的调用
     * selectRenters：查询出租客账号信息
     * @param rentersId:出租客账号id
     * selectRenters：车位模块调用，出租客账号信息
     * @return
     */
    @GetMapping("/selectRenters/{rentersId}")
    @ApiOperation("查询出租客账号信息(车位模块调用)")
    public Result<SelectRentersDTO> selectRenters (@PathVariable("rentersId") Integer rentersId)throws Exception{
        if (rentersId==null) {
            throw new RentersException(RentersExceptionCode.PARAM_NOT_EXISTS);//参数不存在
        }
        Renters renters = rentersService.getById(rentersId);
        if (renters==null) {
            throw new RentersException(RentersExceptionCode.RENTERS_NOT_EXISTS);//出租客账号不存在
        }
        SelectRentersDTO dto = BeanUtil.toBean(renters, SelectRentersDTO.class);
        return Result.getResult(SelectRentersDTO.class).setData(dto).setMessage("查询成功");
    }

    /**
     * 发送出租客注册验证码
     * 参数:
     * (出租客电话):rentersInfoTel
     * (验证码):code
      */
    @PostMapping("/sendRegisterCode")
    @ApiOperation("发送出租客注册验证码")
    public Result sendRegisterCode(String phone)throws Exception{
        rentersService.sendRegisterCode(phone);
        return Result.getResult().setMessage("发送注册验证码成功").setCode(200);
    }
    /**
     * 出租客注册
     * 参数:
     * (出租客账号):rentersAccount
     * (出租客密码):rentersPass
     * (出租客姓名):rentersInfoName
     * (出租客电话):rentersInfoTel
     * @param form
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("出租客注册接口")
    public Result registerRenters(@RequestBody @Valid RegisterForm form) throws Exception {
        rentersService.register(form);
        return Result.getResult().setMessage("注册成功").setCode(200);
    }

    /**
     * 出租客账号登录接口
     * 参数:
     * (出租客账号):rentersAccount
     * (出租客姓名):rentersInfoName
     * (出租客手机):rentersInfoTel
     * (出租客头像):rentersInfoImg
     * (出租客年龄):rentersInfoAge
     * (出租客身份证):rentersInfoCard
     */
    @PostMapping("/login")
    @ApiOperation("出租客登录")
    public Result rentersLogin(@RequestBody @Valid RentersLoginForm form, HttpServletResponse response){
        RentersLoginDTO rentersLoginDTO = rentersService.login(form, response);
        System.out.println(rentersLoginDTO+"参数");
        //将用户信息存进Token
        String refreshToken = JwtUtil.createRentersToken(3 * 24 * 60,"rentersId",rentersLoginDTO.getRentersId());
        System.out.println("参数2"+refreshToken);
        String accessToken = JwtUtil.createRentersToken(30,"rentersId",rentersLoginDTO.getRentersId());
        response.setHeader("authorization", accessToken);
        redisUtil.set(accessToken,refreshToken,3*24*60*60L);
        response.setHeader("Access-Control-Expose-Headers", "authorization");
        return Result.getResult().setMessage("登录成功").setCode(200).setData(rentersLoginDTO);
    }

    /***
     * 出租客退出登录接口
     * 参数:
     * (出租客Id):rentersId
     * @param authorization
     * @return
     * @throws Exception
     */
    @GetMapping("/loginOff.renters")
    @ApiOperation("出租客退出登录")
    public Result loginOff(@RequestHeader("Authorization")String authorization)throws Exception{
        Claims claims = JwtUtil.parseUserToken(authorization);
        Integer rentersId = (Integer) claims.get("rentersId");
        rentersService.loginOff(rentersId);
        return Result.getResult().setMessage("退出登录成功").setCode(200);
    }

    /***
     * 出租客手机登录接口
     * 参数:
     * (手机):phone
     * (验证码):code
     * @param form
     * @param response
     * @return
     * @throws Exception
     */
    @PostMapping("/sendLogin")
    @ApiOperation("出租客验证码登录")
    public Result sendLogin(@RequestBody @Valid RentersPhoneForm form,HttpServletResponse response)throws Exception{
        RentersLoginDTO rentersLoginDTO = rentersService.codeLogin(form, response);
        //将用户信息存进Token
        String refreshToken = JwtUtil.createRentersToken(3 * 24 * 60,"rentersId",rentersLoginDTO.getRentersId());
        String accessToken = JwtUtil.createRentersToken(30,"rentersId",rentersLoginDTO.getRentersId());
        response.setHeader("authorization", accessToken);
        redisUtil.set(accessToken,refreshToken,3*24*60*60L);
        response.setHeader("Access-Control-Expose-Headers", "authorization");
        return Result.getResult().setMessage("登录成功").setCode(200).setData(rentersLoginDTO);
    }

    /***
     *发送出租客登录验证码
     * @param phone 手机号码
     * @return
     */
    @GetMapping("/sendLoginCode")
    @ApiOperation("发送出租客登录验证码")
    public Result sendLoginCode(String phone){
            rentersService.sendLoginCode(phone);
        return Result.getResult().setMessage("发送验证码成功");
    }

    /**
     * 根据ID查询出租客
     * 参数: 出租客Id rentersId
     * 返回: 出租客Id rentersId
     *      出租客账号 rentersAccount
     * 方法: 1.判断rentersId是否存在对应的出租客 若不存在则抛异常
     *      2.判断对应的出租客的状态是否是启用的 若不是则抛异常
     *      3.将出租客放入Dto返回
     * */
    @GetMapping("/selectRentersById")
    @ApiOperation("根据ID查询出租客(订单模块调用)")
    Result<RentersDto> selectRentersById(@RequestParam("rentersId") Integer rentersId){
        RentersDto dto = rentersService.selectRentersById(rentersId);
        return Result.getResult().setMessage("查询成功").setCode(200).setData(dto);
    }

    /***
     * 平台开启出租客账号状态
     * @param rentersId
     * @param authorization
     * @return
     * @throws Exception
     */
    @GetMapping("/enableState.platform")
    @Permission(resources = "/enableState.platform")
    @ApiOperation("平台开启出租客账号状态")
    public Result startState(Integer rentersId,@RequestHeader("Authorization") String authorization)throws Exception{
        Integer platformId = JwtUtil.getPlatformId(authorization);
        rentersService.startState(rentersId,platformId);
        return Result.getResult().setMessage("开启成功").setCode(200);
    }

    /***
     * 平台禁用出租客账号
     * @param rentersId
     * @param authorization
     * @return
     * @throws Exception
     */
    @GetMapping("/disableState.platform")
    @Permission(resources = "/disableState.platform")
    @ApiOperation("平台禁用出租客账号")
    public Result disableState(Integer rentersId,@RequestHeader("Authorization") String authorization)throws Exception{
        Integer platformId = JwtUtil.getPlatformId(authorization);
        rentersService.disableState(rentersId,platformId);
        return Result.getResult().setMessage("禁用成功");
    }

    /***
     * 平台删除出租客账号
     * @param rentersId 出租客id
     * @param authorization 平台id
     * @return
     * @throws Exception
     */
    @GetMapping("/deleteState.platform")
    @Permission(resources = "/deleteState.platform")
    @ApiOperation("平台删除出租客账号")
    public Result deleteState(Integer rentersId,@RequestHeader("Authorization") String authorization)throws Exception{
        Integer platformId = JwtUtil.getPlatformId(authorization);
        rentersService.deleteState(rentersId,platformId);
        return Result.getResult().setMessage("删除成功");
    }

    /***
     * 查询出租客账号
     * @param form
     * @return
     */
     @GetMapping("/selectRenters.platform")
     @ApiOperation("平台查询出租客账号")
    public Result<RentersDto> selectRenters(SelectRenterForm form){
         PageInfo pageInfo = rentersService.selectRenters(form);
         return Result.getResult().setMessage("查询成功").setCode(200).setData(pageInfo);
    }


}

